import pymysql
import schedule
import time
import gpt

db = {
'host': '110.41.147.241',
'user': 'test_ai',
'password': 'nHB3cbLDzAeH74eR',
'database': 'ai_sql',
}

def job():
    connection = pymysql.connect(**db)
    cursor = connection.cursor()
    sql = "select order_on, user_subject, user_title from orderInfo"
    cursor.execute(sql)
    results = cursor.fetchall()
    # print(f'{results}')
    # time.sleep(20)
    if results:
        for row in results:
            order_on = row[0]
            user_subject = row[1]
            user_title = row[2]
            print(f"{order_on}, {user_subject}, {user_title}")
            paper_test = gpt.generate_content(user_subject, user_title)
            paper_text = f'{paper_test}'
            # time.sleep(5)
            print(paper_test)
            if paper_test:
                sql_del = "DELETE FROM orderInfo WHERE order_on = %s;"
                cursor.execute(sql_del, (order_on,))
                connection.commit()
        connection.close()
                # time.sleep(20)
    else:
        print("No data found")
        connection.close()
        # time.sleep(60)

schedule.every(30).minutes.do(job)

while True:
    schedule.run_pending()
    time.sleep(5)